package org.corey.share.auth;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class DefaultRoleAccessStrategory implements RoleAccessStrategory {
	private String roleName;
	private Set<String> allowPlainResources = new HashSet<String>();
	private Set<String> allowExpressResources = new HashSet<String>();

	public DefaultRoleAccessStrategory(String roleName,
			Set<String> allowAccessRequestPaths,
			Set<String> allowAccessRequestPathExpresses) {
		this.roleName = roleName;
		this.allowPlainResources = allowAccessRequestPaths;
		this.allowExpressResources = allowAccessRequestPathExpresses;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public boolean allow(String requestPath) {
		if (allowPlainResources.contains(requestPath)) {
			return true;
		}
		Iterator<String> expressIt = allowExpressResources.iterator();
		while (expressIt.hasNext()) {
			String express = expressIt.next();
			String expressHeader = express.replace("*", "");
			if (requestPath.startsWith(expressHeader)) {
				return true;
			}
		}
		return false;
	}
}
